package cn.afactory.tygz.ui.base.views.subject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.SnsSubjectDetail;
import cn.afactory.tygz.ui.base.adapter.NearByForumsAdapter;
import cn.afactory.tygz.ui.base.views.AutoGridView;
import cn.afactory.tygz.ui.base.views.NoScrollListView;

/**
 * @author xiezhidong251@jk.cn
 * @Description
 * @date 2015/7/16.
 */
public class SubjectContentView {
    private Context mContext;
    private NearByForumsAdapter.OnItemClickListener mItemClickListener;

    private int mIamgeWidth = 0, mIamgeHeight = 0;

    public SubjectContentView(Context ctx, int iamgeWidth, int iamgeHeight) {
        mContext = ctx;
        mIamgeWidth = iamgeWidth;
        mIamgeHeight = iamgeHeight;
    }

    public void setListener(NearByForumsAdapter.OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    private static class ViewHolder {
        TextView personTime;
        ImageView personPortrait;
        TextView personName;
        TextView tvShortDes;
        ImageView ivSex;
        ImageView ivTag;
        TextView tvDel;
        TextView tvLoc;
        TextView tvLike;
        TextView tvChat;
        AutoGridView gvSubject;
        NoScrollListView lvComment;
        RelativeLayout rlll;
        TextView tvAllProfile;
        TextView tvSource;
        LinearLayout llSourceTitle;
        ImageView ivVip;
    }

    public ViewHolder createViewHolder(View convertView) {
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.personPortrait = (ImageView) convertView.findViewById(R.id.subject_submitter_pic);
        viewHolder.personTime = (TextView) convertView.findViewById(R.id.subject_submitter_time);
        viewHolder.personName = (TextView) convertView.findViewById(R.id.subject_submitter_name);
        viewHolder.tvShortDes = (TextView) convertView.findViewById(R.id.subject_shordes);
        viewHolder.tvAllProfile = (TextView) convertView.findViewById(R.id.subject_all_profile);
        viewHolder.ivSex = (ImageView) convertView.findViewById(R.id.subject_sex);
        viewHolder.tvLoc = (TextView) convertView.findViewById(R.id.tv_loc);
        viewHolder.tvLike = (TextView) convertView.findViewById(R.id.tv_like);
        viewHolder.tvChat = (TextView) convertView.findViewById(R.id.tv_chat);
        viewHolder.ivTag = (ImageView) convertView.findViewById(R.id.iv_tag);
        viewHolder.tvDel = (TextView) convertView.findViewById(R.id.subject_delete);
        viewHolder.gvSubject = (AutoGridView) convertView.findViewById(R.id.subject_gridview);
        viewHolder.lvComment = (NoScrollListView) convertView.findViewById(R.id.list_comment);
        viewHolder.rlll = (RelativeLayout) convertView.findViewById(R.id.rl_list);
        viewHolder.tvSource = (TextView) convertView.findViewById(R.id.tv_source);
        viewHolder.llSourceTitle = (LinearLayout) convertView.findViewById(R.id.ll_source);
        viewHolder.ivVip = (ImageView) convertView.findViewById(R.id.subject_vip);
        return viewHolder;
    }

    public View newView(LayoutInflater inflater, ViewGroup parent) {
        View view = inflater.inflate(R.layout.list_item_subject, parent, false);
        return view;
    }

    public View bindView(LayoutInflater mInflater, ViewGroup parent, final SnsSubjectDetail item, View convertView) {
        ViewHolder viewHolder = null;
        if (convertView == null || !(convertView.getTag() instanceof ViewHolder)) {
            convertView = newView(mInflater, parent);
            viewHolder = createViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

//        if (item.subjectInfo != null && item.subjectInfo.textContent != null && !TextUtils.isEmpty(item.subjectInfo.textContent)) {
//            viewHolder.tvShortDes.setVisibility(View.VISIBLE);
//            viewHolder.tvAllProfile.setVisibility(View.GONE);
//            String tmp = item.subjectInfo.textContent;
//            if (item.subjectInfo.textContent.length() >= 200) {
//                tmp = item.subjectInfo.textContent.substring(0, 197) + "...";
//            }
//            viewHolder.tvShortDes.setText(tmp);
//        } else {
//            viewHolder.tvShortDes.setVisibility(View.GONE);
//            viewHolder.tvAllProfile.setVisibility(View.GONE);
//            viewHolder.tvShortDes.setText("");
//        }
//
//        HealthCircleTextUtil.SetLinkClickIntercept(mContext, viewHolder.tvShortDes, mItemClickListener);
//
//        if (item.subjectInfo != null && item.subjectInfo.gmtCreated > 0) {
//            viewHolder.personTime.setText(TimeUtil.getFormatDateTimeStrIgnoreFuture(item.subjectInfo.gmtCreated, mContext));
//        } else {
//            viewHolder.personTime.setText("");
//        }
//
//        if (item.subjectInfo != null && item.subjectInfo.userInfo != null
//                && item.subjectInfo.userInfo.nick != null) {
//            viewHolder.personName.setText(item.subjectInfo.userInfo.nick);
//        } else {
//            viewHolder.personName.setText("");
//        }
//
//        if (item.subjectInfo != null && item.subjectInfo.userInfo != null
//                && item.subjectInfo.userInfo.avatar != null) {
////            BaseImgView.loadimg(viewHolder.personPortrait, ImageUtils.getThumbnailFullPath(item.subjectInfo.userInfo.avatar, (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30) + "x"
////                            + (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30)), R.drawable.ground_touxiang,
////                    R.drawable.ground_touxiang, R.drawable.ground_touxiang, ImageScaleType.EXACTLY,
////                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
////                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30), 0);
//        } else {
////            viewHolder.personPortrait.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ground_touxiang));
//        }
//
//        if(item.subjectInfo != null && item.subjectInfo.userInfo != null
//                && item.subjectInfo.userInfo.userId > 0){
//            viewHolder.personPortrait.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    mItemClickListener.onClickPortrait(item.subjectInfo.userInfo.userId);
//                }
//            });
//        }
//
////        if (item.subjectInfo != null && item.subjectInfo.userInfo != null
////                && item.subjectInfo.userInfo.gender != null) {
////            viewHolder.ivSex.setVisibility(View.VISIBLE);
////            if (item.subjectInfo.userInfo.gender.equals(Gendar.GENDAR_FEMALE)) {
////                viewHolder.ivSex.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tag_female));
////            } else if (item.subjectInfo.userInfo.gender.equals(Gendar.GENDAR_MALE)) {
////                viewHolder.ivSex.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tag_male));
////            } else {
////                viewHolder.ivSex.setVisibility(View.GONE);
////            }
////        } else {
////            viewHolder.ivSex.setVisibility(View.GONE);
////        }
//
//        if (item.subjectInfo != null && item.subjectInfo.pics != null
//                && item.subjectInfo.pics.size() > 0) {
//            viewHolder.gvSubject.setVisibility(View.VISIBLE);
//            SubjectImgGirdAdapter bm = new SubjectImgGirdAdapter(mContext, item.subjectInfo.pics, mIamgeWidth, mIamgeHeight);
//            viewHolder.gvSubject.setAdapter(bm);
//            bm.setListener(new ViewListener() {
//                @Override
//                public void onClick(int position, List<String> mPath) {
//                    mItemClickListener.onClickImage(position, mPath);
//                }
//            });
//
//            viewHolder.gvSubject.setFocusable(false);
//            viewHolder.gvSubject.setEnabled(false);
//            viewHolder.gvSubject.setPressed(false);
//            viewHolder.gvSubject.setClickable(false);
//        } else {
//            viewHolder.gvSubject.setVisibility(View.GONE);
//        }
//
////        if (item.subjectInfo != null && item.subjectInfo.poiInfo != null
////                && item.subjectInfo.poiInfo.detail != null) {
////            viewHolder.tvLoc.setVisibility(View.VISIBLE);
////            viewHolder.tvLoc.setText(item.subjectInfo.poiInfo.detail);
////        } else {
//////            viewHolder.tvLoc.setText("");
////            viewHolder.tvLoc.setVisibility(View.GONE);
////        }
//
//        if (item.subjectInfo != null && item.subjectInfo.praised) {
//            ViewTools.setPraiseView(viewHolder.tvLike, true, item.praiseNum);
//        } else {
//            ViewTools.setPraiseView(viewHolder.tvLike, false, item.praiseNum);
//        }
//        viewHolder.tvLike.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(final View v) {
//                try {
//                    mItemClickListener.onPraise((TextView) v, item);
//                    ViewTools.setPraiseAni((TextView) v);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//
////        if (item.commentNum > 0) {
////            viewHolder.tvChat.setText(String.valueOf(item.commentNum));
////            ViewTools.setTextLeftIcon(viewHolder.tvChat, R.drawable.tag_comment_on);
////        } else {
////            viewHolder.tvChat.setText("评论");
////            ViewTools.setTextLeftIcon(viewHolder.tvChat, R.drawable.tag_comment);
////        }
//
//        if (item.subjectInfo != null && item.subjectInfo.isHot) {
//            viewHolder.ivTag.setVisibility(View.VISIBLE);
//        } else {
//            viewHolder.ivTag.setVisibility(View.GONE);
//        }
//
//        if (item.commentInfoList != null) {
//            viewHolder.rlll.setVisibility(View.VISIBLE);
//            CommentItemadapter bm = new CommentItemadapter(mContext, item.commentInfoList);
//            viewHolder.lvComment.setAdapter(bm);
//            viewHolder.lvComment.setFocusable(false);
//            viewHolder.lvComment.setEnabled(false);
//            viewHolder.lvComment.setPressed(false);
//            viewHolder.lvComment.setClickable(false);
//        } else {
//            viewHolder.rlll.setVisibility(View.GONE);
//        }
//
//        if (item.subjectInfo != null && item.subjectInfo.owner) {
//            viewHolder.tvDel.setVisibility(View.VISIBLE);
//            viewHolder.tvDel.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
////                    HealthCircleControler.showDeleteSubjectDialog(mContext, ((SnsSubjectDetail) getItem(position)).subjectInfo.id);
//                    mItemClickListener.onDelete(item.subjectInfo.id);
//                }
//            });
//
//        } else {
//            viewHolder.tvDel.setVisibility(View.GONE);
//        }
//
////        if(item.subjectInfo != null && !TextUtils.isEmpty(item.subjectInfo.sourceDesc) && !item.subjectInfo.source.equals(HealthSourceType.SNS)){
////            viewHolder.tvSource.setText(item.subjectInfo.sourceDesc);
////            viewHolder.llSourceTitle.setVisibility(View.VISIBLE);
////            if(!TextUtils.isEmpty(item.subjectInfo.source)){
////                viewHolder.tvSource.setOnClickListener(new View.OnClickListener() {
////                    @Override
////                    public void onClick(View v) {
////                        mItemClickListener.onClickSource(item.subjectInfo.source);
////                    }
////                });
////            } else {
////                viewHolder.tvSource.setOnClickListener(null);
////            }
////        } else {
////            viewHolder.llSourceTitle.setVisibility(View.INVISIBLE);
////        }
//
//        if (item.subjectInfo != null && item.subjectInfo.userInfo != null
//                && item.subjectInfo.userInfo.level != null) {
//            if("VERIFIED".equals(item.subjectInfo.userInfo.level)){
//                viewHolder.ivVip.setVisibility(View.VISIBLE);
//            }else {
//                viewHolder.ivVip.setVisibility(View.GONE);
//            }
//        }
        return convertView;
    }

}
